﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DAL;
using BLL;
using BOL;
using DevComponents.DotNetBar;
using System.IO;
using System.Data.SqlClient;
using System.Drawing.Imaging;

namespace QuanLyHangXeKhach
{
    public partial class frmThongTinNV : UserControl
    {
        public string taikhoan;
        string imagename;//lưu tên của hình
        ChucNangNhanVien cn = new ChucNangNhanVien();
        ThongtinNhanVien ttnv = new ThongtinNhanVien();
        DieuKhien dk = new DieuKhien();
        public frmThongTinNV()
        {
            InitializeComponent();           
        }
        #region Điều khiển đóng/mở form
        private DieuKhien _Commands;
        /// <summary>
        /// Gets or sets the commands associated with the control.
        /// </summary>
        public DieuKhien Commands
        {
            get { return _Commands; }
            set
            {
                if (value != _Commands)
                {
                    DieuKhien oldValue = _Commands;
                    _Commands = value;
                    OnCommandsChanged(oldValue, value);
                }
            }
        }
        /// <summary>
        /// Called when Commands property has changed.
        /// </summary>
        /// <param name="oldValue">Old property value</param>
        /// <param name="newValue">New property value</param>
        protected virtual void OnCommandsChanged(DieuKhien oldValue, DieuKhien newValue)
        {
            if (newValue != null)
            {
                btndong.Command = newValue.dongfrmttnv;
            }
            else
            {
                btndong.Command = null;
            }
        }

        #endregion

        private void frmThongTinNV_Load(object sender, EventArgs e)
        {
            cobgioitinh.Items.Add("Nam");
            cobgioitinh.Items.Add("Nữ");
            Loadthongtin();
        }

        private void Loadthongtin()
        {
            AnHien(false);
            List<thongtinnhanvien> thongtin = cn.laynhanvien(taikhoan);
            foreach (var row in thongtin)
            {
                lblmanv.Text = row.ma;
                txtten.Text = row.ten;
                txtdiachi.Text = row.diachi;
                cobgioitinh.Text = row.gioitinh;
                makngaysinh.Text = row.ngaysinh;
                txtcmnd.Text = row.cmnd;
                txtnoisinh.Text = row.noisinh;
                txtsdt.Text = row.sodt;
                //load ảnh
                Image img = Image.FromStream(row.hinhanh);
                if (img == null) return;
                picanhdaidien.SizeMode = PictureBoxSizeMode.StretchImage;
                picanhdaidien.Image = img;
            }
        }

        private void btnduyetdanh_Click(object sender, EventArgs e)
        {
            try
            {
                FileDialog dldlg = new OpenFileDialog();
                dldlg.InitialDirectory = @":d\";
                dldlg.Filter = "Image File(*.jpg;*.bmp;*.gif)|*jpg;*bmp;*gif";
                if (dldlg.ShowDialog() == DialogResult.OK)
                {
                    imagename = dldlg.FileName;
                    Bitmap newimg = new Bitmap(imagename);
                    picanhdaidien.SizeMode = PictureBoxSizeMode.StretchImage;
                    picanhdaidien.Image = (Image)newimg;
                }
                dldlg = null;

            }
            catch (System.ArgumentException ae)
            {
                imagename = " ";
                MessageBox.Show(ae.Message.ToString());

            }
        }

        //khi chưa click sửa
        private void AnHien(bool a)
        {
            txtdiachi.Enabled = a;
            txtten.Enabled = a;            
            makngaysinh.Enabled = a;
            cobgioitinh.Enabled = a;
            btnduyetdanh.Enabled = a;
            txtnoisinh.Enabled = a;
            txtcmnd.Enabled = a;
            txtsdt.Enabled = a;
        }


        //khai báo delegate để update thông tin sau khi sửa lên frm chức năng nhân viên
        public delegate void updatenv();
        public updatenv capnhat;
        private void buttonX1_Click(object sender, EventArgs e)
        {
            AnHien(true);
            if (btnsua.Text == "Chỉnh sửa")
                btnsua.Text = "Lưu";
            else
            {
                MemoryStream stream = new MemoryStream();
                picanhdaidien.Image.Save(stream, ImageFormat.Jpeg);
                ttnv.SuathongTin(lblmanv.Text, txtten.Text, cobgioitinh.Text, dk.catngay(makngaysinh.Text), txtnoisinh.Text, txtdiachi.Text, txtsdt.Text, txtcmnd.Text, stream);
                MessageBoxEx.Show("Sửa thành công !!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                Loadthongtin();
                capnhat();
                btnsua.Text = "Chỉnh sửa";
            }
        }

        private void lbldoimk_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            frmDoiMatKhau frm = new frmDoiMatKhau();
            frm.taikhoandn = taikhoan;
            frm.ShowDialog();
        }
    }
}
